// 登录组件
<template>
  <div class="login-class">
    <div class="login-box">
      <div class="login-logo">
        <img src="../assets/image/logo.png" alt="" />
      </div>
      <el-form
        class="login-form"
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
      >
        <el-form-item prop="username">
          <el-input v-model="loginForm.username">
            <i slot="prefix" class="el-input__icon el-icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" show-password>
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="login('loginForm')"
            >登 录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { min: 5, max: 12, message: "长度在3到12个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 5, max: 12, message: "长度在5到12个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    login(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post("login", this.loginForm);
          if (res.meta.status === 200) {
            this.$message.success("登陆成功");
            window.sessionStorage.setItem("token", res.data.token);
            this.$router.push("/home");
          } else {
            this.$message.error("用户名不存在");
          }
        } else {
          this.$message.warning("请输入正确的格式！");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-class {
  background-color: rgb(255, 242, 242);
  height: 100%;
}
.login-box {
  box-sizing: border-box;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
  height: 300px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
}
.login-logo {
  position: absolute;
  left: 50%;
  top: 0;
  transform: translate(-50%, -50%);
  width: 50px;
  height: 50px;
  border-radius: 50%;
  overflow: hidden;
  img {
    width: 100%;
    height: 100%;
  }
}
.login-form {
  margin-top: 50px;
}
.el-button {
  width: 100%;
}
</style>